package com.example.assetmanagement.controller;

import com.example.assetmanagement.common.ApiResponse;
import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.MaintenancePlan;
import com.example.assetmanagement.service.MaintenancePlanService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/maintenance-plan")
@RequiredArgsConstructor
public class MaintenancePlanController {

    private final MaintenancePlanService planService;

    @GetMapping("/list")
    public ApiResponse<PageResponse<MaintenancePlan>> list(@Valid PageRequest pageRequest,
                                                           @RequestParam(required = false) String type,
                                                           @RequestParam(required = false) Integer status,
                                                           @RequestParam(required = false) String startDate,
                                                           @RequestParam(required = false) String endDate) {
        return ApiResponse.success(planService.list(pageRequest, type, status, startDate, endDate));
    }

    @PostMapping
    public ApiResponse<?> add(@RequestBody MaintenancePlan plan){
        planService.add(plan);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}")
    public ApiResponse<?> update(@PathVariable Long id,@RequestBody MaintenancePlan plan){
        planService.update(id, plan);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> delete(@PathVariable Long id){
        planService.delete(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/batch")
    public ApiResponse<?> batchDelete(@RequestBody List<Long> ids){
        planService.batchDelete(ids);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}/status")
    public ApiResponse<?> updateStatus(@PathVariable Long id,
                                       @RequestParam Integer status,
                                       @RequestParam(defaultValue = "0") Integer progress){
        planService.updateStatus(id, status, progress);
        return ApiResponse.success(null);
    }
} 